import {
  login,
  logout,
  getInfo,
  getTimeStamp,
  changePassword,
  resetRamKey,
  getRamKey
} from '@/api/user'
import {
  getToken,
  setToken,
  removeToken,
  fnAddDynamicMenuRoutes
} from '@/utils/auth'
import { menuDataTranslate } from '@/utils'
import { encryptPassword } from '@/utils/auth'
import { resetRouter } from '@/router'
import router from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    username: '',
    logoUrl: '',
    avatar: '',
    menuList: [],
    orgId: '',
    userInfo: {
      userId: 194,
      characterCode: 'OPERATOR',
      keyId: 95,
      manufacturerCode: null,
      manufacturerName: null,
      status: 1,
      oid: 'F38EA196D034667FD9D18E884DD00DAB4CAB54E1990E7944D98E3814AE91C898',
      operatorType: 'NORMAL',
      mapType: 'MAP_BOX',
      mapKey:
        'pk.eyJ1IjoieGlhb2hhaXFpYW5nIiwiYSI6ImNsMXlmYWxyMTBjNTEza281aDY2OHc3eTMifQ.DixPNKsuzsEV740Mh-9NHA',
      mapToken: null,
      levelCode: 'ENTERPRISE_LEVEL',
      developerType: null,
      expireTime: 1706544000,
      countryCode: 'CHN',
      lang: null,
      utcTime: null,
      username: 'AppSigner-inte',
      email: 'liuyit@newlandnpt.com',
      logoUrl: '17.png',
      name: 'AppSigner',
      loginTime: 1695628311,
      gender: null,
      address: null,
      token: '372BB972-D2A1-4B50-A6A1-E843737AEA0E',
      orgId: 280,
      orgCode: '2205160236',
      orgName: 'AppSigner',
      managerFlag: 1,
      topOrgFlag: 1,
      dateFormat: 'yyyy/MM/dd',
      characterStatus: 1,
      loginDevSn: null,
      needPasswdChange: 0,
      sessionExpireTime: 14400,
      menuFunctionList: [
        { menuFuncCode: '124012.QUERY', menuId: 124012, funName: 'View' },
        { menuFuncCode: '124012.MANAGE', menuId: 124012, funName: 'Manage' },
        { menuFuncCode: '12408000.QUERY', menuId: 12408000, funName: 'View' },
        {
          menuFuncCode: '12408000.MANAGE',
          menuId: 12408000,
          funName: 'Manage'
        },
        { menuFuncCode: '12408004.QUERY', menuId: 12408004, funName: 'View' },
        {
          menuFuncCode: '12408004.MANAGE',
          menuId: 12408004,
          funName: 'Manage'
        },
        { menuFuncCode: '12420000.QUERY', menuId: 12420000, funName: 'View' },
        {
          menuFuncCode: '12420000.MANAGE',
          menuId: 12420000,
          funName: 'Manage'
        },
        { menuFuncCode: '12401001.QUERY', menuId: 12401001, funName: 'View' },
        { menuFuncCode: '12401002.QUERY', menuId: 12401002, funName: 'View' },
        { menuFuncCode: '12401003.QUERY', menuId: 12401003, funName: 'View' },
        {
          menuFuncCode: '12401003.MANAGE',
          menuId: 12401003,
          funName: 'Manage'
        },
        { menuFuncCode: '12401006.QUERY', menuId: 12401006, funName: 'View' },
        { menuFuncCode: '12401607.QUERY', menuId: 12401607, funName: 'View' },
        {
          menuFuncCode: '12401607.MANAGE',
          menuId: 12401607,
          funName: 'Manage'
        },
        { menuFuncCode: '12401608.QUERY', menuId: 12401608, funName: 'View' },
        {
          menuFuncCode: '12401608.MANAGE',
          menuId: 12401608,
          funName: 'Manage'
        },
        { menuFuncCode: '12401609.QUERY', menuId: 12401609, funName: 'View' },
        {
          menuFuncCode: '12401609.MANAGE',
          menuId: 12401609,
          funName: 'Manage'
        },
        { menuFuncCode: '12401610.QUERY', menuId: 12401610, funName: 'View' },
        {
          menuFuncCode: '12401610.MANAGE',
          menuId: 12401610,
          funName: 'Manage'
        },
        { menuFuncCode: '12401901.QUERY', menuId: 12401901, funName: 'View' },
        { menuFuncCode: '12401902.QUERY', menuId: 12401902, funName: 'View' },
        { menuFuncCode: '12401903.QUERY', menuId: 12401903, funName: 'View' },
        { menuFuncCode: '12401904.QUERY', menuId: 12401904, funName: 'View' },
        {
          menuFuncCode: '12401002.ORG_MANAGE',
          menuId: 12401002,
          funName: 'Org_Manage'
        },
        {
          menuFuncCode: '12401006.MANAGE',
          menuId: 12401006,
          funName: 'Manage'
        },
        {
          menuFuncCode: '12401606.MANAGE',
          menuId: 12401606,
          funName: 'Manage'
        },
        { menuFuncCode: '12401606.QUERY', menuId: 12401606, funName: 'View' },
        { menuFuncCode: '12401007.QUERY', menuId: 12401007, funName: 'View' },
        {
          menuFuncCode: '12401007.MANAGE',
          menuId: 12401007,
          funName: 'Manage'
        },
        { menuFuncCode: '12420001.QUERY', menuId: 12420001, funName: 'View' },
        {
          menuFuncCode: '12401001.ESTATE_MANAGE',
          menuId: 12401001,
          funName: 'Estate_Manage'
        },
        {
          menuFuncCode: '12401001.REMOTE_COMMAND',
          menuId: 12401001,
          funName: 'Remote_Command'
        },
        {
          menuFuncCode: '12401001.HARDWARE_DIAGNOSTICS',
          menuId: 12401001,
          funName: 'Hardware_Diagnostics'
        },
        {
          menuFuncCode: '12401001.LOG_MANAGE',
          menuId: 12401001,
          funName: 'Log_Manage'
        },
        {
          menuFuncCode: '12401002.MRCH_MANAGE',
          menuId: 12401002,
          funName: 'Mrch_Manage'
        },
        {
          menuFuncCode: '12401002.ESTATE_MANAGE',
          menuId: 12401002,
          funName: 'Estate_Manage'
        },
        {
          menuFuncCode: '12401004.APP_VIEW',
          menuId: 12401004,
          funName: 'App_View'
        },
        {
          menuFuncCode: '12401004.APP_MANAGE',
          menuId: 12401004,
          funName: 'App_Manage'
        },
        {
          menuFuncCode: '12401004.APP_UPDATE_MANAGE',
          menuId: 12401004,
          funName: 'App_Update_Manage'
        },
        {
          menuFuncCode: '12401004.BOOT_RES_VIEW',
          menuId: 12401004,
          funName: 'Boot_Res_View'
        },
        {
          menuFuncCode: '12401004.BOOT_RES_MANAGE',
          menuId: 12401004,
          funName: 'Boot_Res_Manage'
        },
        {
          menuFuncCode: '12401609.TRACE_MONITORING',
          menuId: 12401609,
          funName: 'Trace_Monitoring'
        },
        {
          menuFuncCode: '12401611.MANAGE',
          menuId: 12401611,
          funName: 'Manage'
        },
        { menuFuncCode: '12401611.QUERY', menuId: 12401611, funName: 'View' },
        {
          menuFuncCode: '12401001.CHANGE_DEVICE',
          menuId: 12401001,
          funName: 'Change_Device'
        },
        { menuFuncCode: '12408006.QUERY', menuId: 12408006, funName: 'View' },
        { menuFuncCode: '12408008.QUERY', menuId: 12408008, funName: 'View' },
        {
          menuFuncCode: '12408008.MANAGE',
          menuId: 12408008,
          funName: 'Manage'
        },
        { menuFuncCode: '12408090.QUERY', menuId: 12408090, funName: 'View' },
        {
          menuFuncCode: '12408090.MANAGE',
          menuId: 12408090,
          funName: 'Manage'
        },
        { menuFuncCode: '12448002.LIST', menuId: 12448002, funName: 'LIST' },
        { menuFuncCode: '12448002.ADD', menuId: 12448002, funName: 'ADD' },
        { menuFuncCode: '12448002.EDIT', menuId: 12448002, funName: 'EDIT' },
        {
          menuFuncCode: '12448002.DELETE',
          menuId: 12448002,
          funName: 'DELETE'
        },
        { menuFuncCode: '12444000.QUERY', menuId: 12444000, funName: 'QUERY' },
        {
          menuFuncCode: '12444000.IMPORT',
          menuId: 12444000,
          funName: 'IMPORT'
        },
        {
          menuFuncCode: '12444000.IMPORT_HISTORY_QUERY',
          menuId: 12444000,
          funName: 'IMPORT HISTORY'
        },
        {
          menuFuncCode: '12444000.IMPORT_HISTORY_TERMINAL_QUERY',
          menuId: 12444000,
          funName: 'IMPORT HISTORY TERMINAL QUERY'
        },
        {
          menuFuncCode: '12444000.IMPORT_HISTORY_DETAIL',
          menuId: 12444000,
          funName: 'IMPORT HISTORY DETAIL'
        },
        {
          menuFuncCode: '12444000.TRANSFER',
          menuId: 12444000,
          funName: 'TRANSFER'
        },
        {
          menuFuncCode: '12444000.RECEIVE',
          menuId: 12444000,
          funName: 'RECEIVE'
        },
        {
          menuFuncCode: '12444000.TRANSFER_HISTORY_QUERY',
          menuId: 12444000,
          funName: 'TRANSFER HISTORY'
        },
        {
          menuFuncCode: '12444000.TRANSFER_TERMINAL_QUERY',
          menuId: 12444000,
          funName: 'TRANSFER TERMINAL QUERY'
        },
        {
          menuFuncCode: '12444000.TRANSFER_REVOKE',
          menuId: 12444000,
          funName: 'TRANSFER REVOKE'
        },
        {
          menuFuncCode: '12441000.QUERY',
          menuId: 12441000,
          funName: 'TERMINAL QUERY'
        },
        {
          menuFuncCode: '12441000.RUNNING_INFO',
          menuId: 12441000,
          funName: 'TERMINAL RUNNING INFO'
        },
        {
          menuFuncCode: '12441000.PUSH_HISTORY_QUERY',
          menuId: 12441000,
          funName: 'TERMINAL PUSH HISTORY QUERY'
        },
        {
          menuFuncCode: '12441000.UPDATE_HISTORY_QUERY',
          menuId: 12441000,
          funName: 'TERMINAL UPDATE HISTORY QUERY'
        },
        {
          menuFuncCode: '12441000.REMOTE_COMMAND_HISTORY',
          menuId: 12441000,
          funName: 'TERMINAL REMOTE COMMAND HISTORY'
        },
        {
          menuFuncCode: '12441000.LOCK',
          menuId: 12441000,
          funName: 'TERMINAL LOCK'
        },
        {
          menuFuncCode: '12441000.UNLOCK',
          menuId: 12441000,
          funName: 'TERMINAL UNLOCK'
        },
        {
          menuFuncCode: '12441002.RECOVERY',
          menuId: 12441002,
          funName: 'TERMINAL RECOVERY'
        },
        {
          menuFuncCode: '12441002.SYNC_PARAMS',
          menuId: 12441002,
          funName: 'TERMINAL SYNC ONLINE PARAMS'
        },
        {
          menuFuncCode: '12441002.SYNC_KEY',
          menuId: 12441002,
          funName: 'TERMINAL SYNC KEY'
        },
        {
          menuFuncCode: '12441002.SYNC_APP',
          menuId: 12441002,
          funName: 'TERMINAL SYNC APP'
        },
        {
          menuFuncCode: '12441002.SYNC_DATA',
          menuId: 12441002,
          funName: 'TERMINAL SYNC TERMINAL DATA'
        },
        {
          menuFuncCode: '12441000.DELETE',
          menuId: 12441000,
          funName: 'DELETE'
        },
        { menuFuncCode: '12441000.EDIT', menuId: 12441000, funName: 'EDIT' },
        { menuFuncCode: '12441000.LOST', menuId: 12441000, funName: 'LOST' },
        {
          menuFuncCode: '12441000.WITHDRAW',
          menuId: 12441000,
          funName: 'WITHDRAW'
        },
        {
          menuFuncCode: '12441000.REPAIR',
          menuId: 12441000,
          funName: 'REPAIR'
        },
        {
          menuFuncCode: '12441000.RETRIEVE',
          menuId: 12441000,
          funName: 'RETRIEVE'
        },
        {
          menuFuncCode: '12441002.PUSH_APP',
          menuId: 12441002,
          funName: 'PUSH APP'
        },
        {
          menuFuncCode: '12441002.PUSH_OTA',
          menuId: 12441002,
          funName: 'PUSH OTA'
        },
        {
          menuFuncCode: '12441002.LAUNCH_APP',
          menuId: 12441002,
          funName: 'LAUNCH APP'
        },
        {
          menuFuncCode: '12441002.CLOSE_APP',
          menuId: 12441002,
          funName: 'CLOSE APP'
        },
        {
          menuFuncCode: '12441002.UNINSTALL_APP',
          menuId: 12441002,
          funName: 'UNINSTALL APP'
        },
        {
          menuFuncCode: '12441002.SET_APP',
          menuId: 12441002,
          funName: 'SET APP'
        },
        {
          menuFuncCode: '12441002.SET_SYSTEM',
          menuId: 12441002,
          funName: 'SET SYSTEM'
        },
        { menuFuncCode: '12441004.QUERY', menuId: 12441004, funName: 'QUERY' },
        { menuFuncCode: '12441004.ADD', menuId: 12441004, funName: 'ADD' },
        { menuFuncCode: '12441004.EDIT', menuId: 12441004, funName: 'EDIT' },
        {
          menuFuncCode: '12441004.DELETE',
          menuId: 12441004,
          funName: 'DELETE'
        },
        {
          menuFuncCode: '12441004.TERMINAL_DEPLOYMENT',
          menuId: 12441004,
          funName: 'TERMINAL DEPLOYMENT'
        },
        {
          menuFuncCode: '12441004.TERMINAL_WITHDRAW',
          menuId: 12441004,
          funName: 'TERMINAL WITHDRAW'
        },
        {
          menuFuncCode: '12401618.MANAGE',
          menuId: 12401618,
          funName: 'Manage'
        },
        { menuFuncCode: '12401618.QUERY', menuId: 12401618, funName: 'View' },
        {
          menuFuncCode: '12441618.SPEAKER',
          menuId: 12441618,
          funName: '语音播报'
        },
        {
          menuFuncCode: '12401616.MANAGE',
          menuId: 12401616,
          funName: 'Manage'
        },
        { menuFuncCode: '12401616.QUERY', menuId: 12401616, funName: 'View' },
        { menuFuncCode: '12401906.QUERY', menuId: 12401906, funName: 'View' },
        {
          menuFuncCode: '12401906.MANAGE',
          menuId: 12401906,
          funName: 'Manage'
        },
        {
          menuFuncCode: '12445000.VERIFY',
          menuId: 12445000,
          funName: 'Verify'
        },
        {
          menuFuncCode: '12441002.CUSTOM_COMMAND',
          menuId: 12441002,
          funName: 'CUSTOM COMMAND'
        },
        { menuFuncCode: '12441006.QUERY', menuId: 12441006, funName: 'View' },
        { menuFuncCode: '12441006.ADD', menuId: 12441006, funName: 'Add' },
        {
          menuFuncCode: '12441006.DELETE',
          menuId: 12441006,
          funName: 'Delete'
        },
        {
          menuFuncCode: '12441006.TERMINAL_MANAGE',
          menuId: 12441006,
          funName: 'Terminal Manage'
        },
        {
          menuFuncCode: '12441006.APP_MANAGE',
          menuId: 12441006,
          funName: 'App Manage'
        },
        {
          menuFuncCode: '12441006.OTA_MANAGE',
          menuId: 12441006,
          funName: 'OTA Manage'
        },
        {
          menuFuncCode: '12401802.MANAGE',
          menuId: 12401802,
          funName: 'Manage'
        },
        { menuFuncCode: '12401802.QUERY', menuId: 12401802, funName: 'View' },
        {
          menuFuncCode: '1240180000.MANAGE',
          menuId: 1240180000,
          funName: 'Manage'
        },
        {
          menuFuncCode: '1240180000.QUERY',
          menuId: 1240180000,
          funName: 'View'
        },
        {
          menuFuncCode: '1240180002.MANAGE',
          menuId: 1240180002,
          funName: 'Manage'
        },
        {
          menuFuncCode: '1240180002.QUERY',
          menuId: 1240180002,
          funName: 'View'
        },
        {
          menuFuncCode: '1240180004.MANAGE',
          menuId: 1240180004,
          funName: 'Manage'
        },
        {
          menuFuncCode: '1240180004.QUERY',
          menuId: 1240180004,
          funName: 'View'
        },
        {
          menuFuncCode: '124012.FLYRECEIPT',
          menuId: 124012,
          funName: 'Flyreceipt'
        },
        {
          menuFuncCode: '12401004.FLYRECEIPT',
          menuId: 12401004,
          funName: 'Flyreceipt'
        },
        {
          menuFuncCode: '12401002.FLYRECEIPT',
          menuId: 12401002,
          funName: 'Flyreceipt'
        },
        { menuFuncCode: '12484200.QUERY', menuId: 12484200, funName: 'view' },
        {
          menuFuncCode: '12484200.CONFIRM',
          menuId: 12484200,
          funName: 'confirm_or_refuse'
        },
        { menuFuncCode: '12484202.QUERY', menuId: 12484202, funName: 'view' },
        { menuFuncCode: '12484290.QUERY', menuId: 12484290, funName: 'view' },
        { menuFuncCode: '12484292.QUERY', menuId: 12484292, funName: 'view' },
        {
          menuFuncCode: '12484292.MANAGE',
          menuId: 12484292,
          funName: 'manage'
        },
        {
          menuFuncCode: '12488000.MANAGE',
          menuId: 12488000,
          funName: 'manage'
        },
        { menuFuncCode: '12488000.QUERY', menuId: 12488000, funName: 'view' },
        {
          menuFuncCode: '12488004.MANAGE',
          menuId: 12488004,
          funName: 'manage'
        },
        { menuFuncCode: '12488004.QUERY', menuId: 12488004, funName: 'view' },
        { menuFuncCode: '12488006.QUERY', menuId: 12488006, funName: 'view' },
        {
          menuFuncCode: '12401620.PARAM_PERMISSION_CONTROL',
          menuId: 12401620,
          funName: 'Param_Permission_Control'
        },
        {
          menuFuncCode: '12401620.DESIGN',
          menuId: 12401620,
          funName: 'Template_Design'
        },
        {
          menuFuncCode: '12401620.CONFIGURATION',
          menuId: 12401620,
          funName: 'Param_Configuration'
        },
        { menuFuncCode: '12401620.QUERY', menuId: 12401620, funName: 'View' },
        { menuFuncCode: '12441618.MIS', menuId: 12441618, funName: 'Mis push' },
        {
          menuFuncCode: '12441618.QUERY',
          menuId: 12441618,
          funName: 'Query history'
        },
        {
          menuFuncCode: '12408010.ROLE_MANAGE',
          menuId: 12408010,
          funName: 'Role_Manage'
        },
        {
          menuFuncCode: '12408010.USER_MANAGE',
          menuId: 12408010,
          funName: 'User_Manage'
        },
        { menuFuncCode: '12408010.QUERY', menuId: 12408010, funName: 'View' },
        {
          menuFuncCode: '12441618.QRCODE',
          menuId: 12441618,
          funName: 'QR Code push'
        },
        {
          menuFuncCode: '12401001.TERMINAL_SETTINGS',
          menuId: 12401001,
          funName: 'Terminal_Settings'
        },
        {
          menuFuncCode: '12401001.APP_SETTINGS',
          menuId: 12401001,
          funName: 'APP_Settings'
        },
        {
          menuFuncCode: '12401618.CORPUS_MANAGE',
          menuId: 12401618,
          funName: 'Corpus_Manage'
        }
      ],
      limitMenuFuncCodeList: ['12401611.MANAGE', '12401611.QUERY'],
      platformInfoList: [
        {
          platCode: 'TOMS',
          menuList: [
            {
              menuId: 124010,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 0,
              menuTitle: 'TMS',
              showFlag: '1',
              href: '',
              sort: 124010,
              icon: 'tms'
            },
            {
              menuId: 124012,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 0,
              menuTitle: 'App Store',
              showFlag: '1',
              href: '/appstore/index',
              sort: 124012,
              icon: 'appstore'
            },
            {
              menuId: 124018,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 0,
              menuTitle: 'Fly Receipt',
              showFlag: '1',
              href: null,
              sort: 124014,
              icon: 'merchant-service'
            },
            {
              menuId: 124016,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 0,
              menuTitle: 'Advance Service',
              showFlag: '1',
              href: null,
              sort: 124016,
              icon: 'vas'
            },
            {
              menuId: 124019,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 0,
              menuTitle: 'Data Center',
              showFlag: '1',
              href: null,
              sort: 124019,
              icon: 'data-center'
            },
            {
              menuId: 124200,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 0,
              menuTitle: 'Estate',
              showFlag: '1',
              href: null,
              sort: 124020,
              icon: 'asset'
            },
            {
              menuId: 124080,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 0,
              menuTitle: 'System',
              showFlag: '1',
              href: null,
              sort: 124080,
              icon: 'user'
            },
            {
              menuId: 12401001,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 124010,
              menuTitle: 'Terminal',
              showFlag: '1',
              href: '/terminal/index',
              sort: 12401001,
              icon: null
            },
            {
              menuId: 12401002,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 124010,
              menuTitle: 'Merchant',
              showFlag: '1',
              href: '/merchant/index',
              sort: 12401002,
              icon: null
            },
            {
              menuId: 12401003,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 124010,
              menuTitle: 'Installation Template',
              showFlag: '1',
              href: '/group/index',
              sort: 12401003,
              icon: null
            },
            {
              menuId: 12401004,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 124010,
              menuTitle: 'Resource',
              showFlag: '1',
              href: '/application/index',
              sort: 12401004,
              icon: null
            },
            {
              menuId: 12401007,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 124010,
              menuTitle: 'Group',
              showFlag: '1',
              href: '/batch/index',
              sort: 20000000,
              icon: null
            },
            {
              menuId: 12401006,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 124010,
              menuTitle: 'Renewal Schedule',
              showFlag: '1',
              href: 'schedule/index',
              sort: 20000001,
              icon: null
            },
            {
              menuId: 12401606,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 124016,
              menuTitle: 'FlyKey',
              showFlag: '1',
              href: '/fly-key/index',
              sort: 12401606,
              icon: null
            },
            {
              menuId: 12401607,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 124016,
              menuTitle: 'FlyDesk',
              showFlag: '1',
              href: '/flydesk/index',
              sort: 12401607,
              icon: null
            },
            {
              menuId: 12401608,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 124016,
              menuTitle: 'Pre-warning',
              showFlag: '1',
              href: '/vas/prewarning/index',
              sort: 12401608,
              icon: null
            },
            {
              menuId: 12401609,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 124016,
              menuTitle: 'Geo-Fencing',
              showFlag: '1',
              href: '/geo/index',
              sort: 12401609,
              icon: null
            },
            {
              menuId: 12401610,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 124016,
              menuTitle: 'Custom Command',
              showFlag: '1',
              href: '/vas/custom-command',
              sort: 12401610,
              icon: null
            },
            {
              menuId: 12401611,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 124016,
              menuTitle: 'Payment Module',
              showFlag: '1',
              href: '/vas/payment/index',
              sort: 12401611,
              icon: null
            },
            {
              menuId: 12401616,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 124016,
              menuTitle: 'UPG Task',
              showFlag: '1',
              href: '/vas/upg-task/index',
              sort: 12401616,
              icon: null
            },
            {
              menuId: 12401618,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 124016,
              menuTitle: 'FlyIoT',
              showFlag: '1',
              href: '/vas/iot/index',
              sort: 12401618,
              icon: null
            },
            {
              menuId: 12401620,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 124016,
              menuTitle: 'Fly Parameter',
              showFlag: '1',
              href: '/vas/fly-parameter/index',
              sort: 12401620,
              icon: null
            },
            {
              menuId: 12401800,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 124018,
              menuTitle: 'Fly Receipt',
              showFlag: '1',
              href: '/merchant-service/index',
              sort: 12401800,
              icon: null
            },
            {
              menuId: 12401802,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 124018,
              menuTitle: 'Fly Receipt',
              showFlag: '1',
              href: '/merchant-service/merchant-list',
              sort: 12401802,
              icon: null
            },
            {
              menuId: 12401901,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 124019,
              menuTitle: 'Device analyze',
              showFlag: '1',
              href: '/datacenter/device-analyze',
              sort: 12401901,
              icon: null
            },
            {
              menuId: 12401902,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 124019,
              menuTitle: 'Merchant analyze',
              showFlag: '1',
              href: '/datacenter/merchant-analyze',
              sort: 12401902,
              icon: null
            },
            {
              menuId: 12401903,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 124019,
              menuTitle: 'Application analyze',
              showFlag: '1',
              href: '/datacenter/application-analyze',
              sort: 12401903,
              icon: null
            },
            {
              menuId: 12401904,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 124019,
              menuTitle: 'Traffic analyze',
              showFlag: '1',
              href: '/datacenter/traffic-analyze',
              sort: 12401904,
              icon: null
            },
            {
              menuId: 12401906,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 124019,
              menuTitle: 'Dynamic Chart',
              showFlag: '1',
              href: '/datacenter/dynamic-chart',
              sort: 12401906,
              icon: null
            },
            {
              menuId: 12408000,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 124080,
              menuTitle: 'User',
              showFlag: '1',
              href: '/user/index',
              sort: 12408000,
              icon: null
            },
            {
              menuId: 12408004,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 124080,
              menuTitle: 'Role',
              showFlag: '1',
              href: '/role/index',
              sort: 12408004,
              icon: null
            },
            {
              menuId: 12408010,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 124080,
              menuTitle: 'Permission',
              showFlag: '1',
              href: '/permission/index',
              sort: 12408008,
              icon: null
            },
            {
              menuId: 12408008,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 124080,
              menuTitle: 'Notification Template',
              showFlag: '1',
              href: '/notification-template/index',
              sort: 12408008,
              icon: null
            },
            {
              menuId: 12408090,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 124080,
              menuTitle: 'RAM',
              showFlag: '1',
              href: '/ram/index',
              sort: 12408090,
              icon: null
            },
            {
              menuId: 12408006,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 124080,
              menuTitle: 'Log',
              showFlag: '1',
              href: '/log/index',
              sort: 12408099,
              icon: null
            },
            {
              menuId: 12420000,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 124200,
              menuTitle: 'Batch Management',
              showFlag: '1',
              href: '/estate/batch',
              sort: 12420000,
              icon: null
            },
            {
              menuId: 12420001,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 124200,
              menuTitle: 'Estate Operator Records',
              showFlag: '1',
              href: '/estate/estate-operator-records',
              sort: 12420001,
              icon: ''
            },
            {
              menuId: 1240180000,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 12401800,
              menuTitle: 'FlyReceipt',
              showFlag: '1',
              href: '/merchant-service/receipt-app',
              sort: 1240180000,
              icon: null
            },
            {
              menuId: 1240180002,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 12401800,
              menuTitle: 'FlyReceipt',
              showFlag: '1',
              href: '/merchant-service/flow-list',
              sort: 1240180002,
              icon: null
            },
            {
              menuId: 1240180004,
              platCode: 'TOMS',
              characterCode: 'OPERATOR',
              parentMenuId: 12401800,
              menuTitle: 'FlyReceipt',
              showFlag: '1',
              href: '/merchant-service/data-dictionary',
              sort: 1240180004,
              icon: null
            }
          ]
        },
        {
          platCode: 'FLYCARE',
          menuList: [
            {
              menuId: 124842,
              platCode: 'FLYCARE',
              characterCode: 'OPERATOR',
              parentMenuId: 0,
              menuTitle: 'Service',
              showFlag: '1',
              href: null,
              sort: 124842,
              icon: 'document_review'
            },
            {
              menuId: 124880,
              platCode: 'FLYCARE',
              characterCode: 'OPERATOR',
              parentMenuId: 0,
              menuTitle: 'System',
              showFlag: '1',
              href: null,
              sort: 124880,
              icon: 'user'
            },
            {
              menuId: 12484200,
              platCode: 'FLYCARE',
              characterCode: 'OPERATOR',
              parentMenuId: 124842,
              menuTitle: 'Service Report',
              showFlag: '1',
              href: '/service-report/index',
              sort: 12484200,
              icon: null
            },
            {
              menuId: 12484202,
              platCode: 'FLYCARE',
              characterCode: 'OPERATOR',
              parentMenuId: 124842,
              menuTitle: 'Bill',
              showFlag: '1',
              href: '/bill/index',
              sort: 12484202,
              icon: null
            },
            {
              menuId: 12484290,
              platCode: 'FLYCARE',
              characterCode: 'OPERATOR',
              parentMenuId: 124842,
              menuTitle: 'Statistics',
              showFlag: '0',
              href: '/statistical/index',
              sort: 12484290,
              icon: null
            },
            {
              menuId: 12484292,
              platCode: 'FLYCARE',
              characterCode: 'OPERATOR',
              parentMenuId: 124842,
              menuTitle: 'Notification Manage',
              showFlag: '1',
              href: '/notification/index',
              sort: 12484292,
              icon: null
            },
            {
              menuId: 12488000,
              platCode: 'FLYCARE',
              characterCode: 'OPERATOR',
              parentMenuId: 124880,
              menuTitle: 'User',
              showFlag: '1',
              href: '/user/index',
              sort: 12488000,
              icon: null
            },
            {
              menuId: 12488004,
              platCode: 'FLYCARE',
              characterCode: 'OPERATOR',
              parentMenuId: 124880,
              menuTitle: 'Role',
              showFlag: '1',
              href: '/role/index',
              sort: 12488004,
              icon: null
            },
            {
              menuId: 12488006,
              platCode: 'FLYCARE',
              characterCode: 'OPERATOR',
              parentMenuId: 124880,
              menuTitle: 'Logs',
              showFlag: '1',
              href: '/log/index',
              sort: 12488006,
              icon: null
            }
          ]
        }
      ],
      characterBindOuterList: [
        {
          characterBindOuterId: 3,
          characterCode: 'OPERATOR',
          keyId: 95,
          orgId: 280,
          outerPlatCode: 'OA',
          outerCharacterCode: 'OPERATOR',
          outerKeyId: 'KH20144037',
          outerOrgId: null,
          outerUsername: null,
          outerPassword: null,
          extend1: '重庆市城投金卡信息产业（集团）股份有限公司',
          extend2: null,
          remark: null,
          creTime: 1669703146,
          status: 1
        },
        {
          characterBindOuterId: 251,
          characterCode: 'OPERATOR',
          keyId: 95,
          orgId: 280,
          outerPlatCode: 'MSS',
          outerCharacterCode: 'OPERATOR',
          outerKeyId: '4028a3868584a603018584ba4ce30000',
          outerOrgId: '000',
          outerUsername: 'AppSigner-inte',
          outerPassword: 'tUzs3HxwHPPNJ+n4Ae+cMA==',
          extend1: '0',
          extend2: '33727481',
          remark: null,
          creTime: 1672969080,
          status: 1
        }
      ]
    },
    isAddDynamicMenuRoutes: false,
    keys: {
      storage: {
        username: 'USER_NAME',
        lang: 'LANG'
      }
    },
    btnPermissions: []
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
    localStorage.removeItem(state.keys.storage.username)
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_USERNAME: (state, username) => {
    state.username = username
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_LOGO_URL: (state, name) => {
    state.logoUrl = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_MENU_LIST: (state, menuList) => {
    state.menuList = menuList
    state.isAddDynamicMenuRoutes = true
  },
  SET_ORG_ID: (state, orgId) => {
    state.orgId = orgId
  },
  SET_USER_INFO: (state, info) => {
    state.userInfo = info
    localStorage.setItem(state.keys.storage.username, JSON.stringify(info))
  },
  SET_PERMISSIONS: (state, permissions) => {
    state.btnPermissions = permissions.map(
      (permission) => permission.menuFuncCode
    )
  }
}

const actions = {
  // user login
  // async login(store, userInfo) {
  //   const { username, password, captcha } = userInfo
  //   const { timestamp } = await getTimeStamp()
  //   const json = JSON.stringify({
  //     key: timestamp,
  //     password
  //   })
  //   const encryptedPassword = encryptPassword(json)

  //   const _userInfo = await login({
  //     username: username.trim(),
  //     password,
  //     code: captcha
  //   })

  //   const { characterStatus, token } = _userInfo

  //   if (characterStatus !== 1) {
  //     if (characterStatus === 2) {
  //       store.dispatch('resetToken').then(() => {
  //         window.location.href = `http://localhost:8080/#/account-status?token=${response.token}`
  //       })
  //     } else if (characterStatus === 3) {
  //       store.dispatch('resetToken').then(() => {
  //         window.location.href = `http://localhost:8080/#/resubmit-form?token=${response.token}`
  //       })
  //     }
  //   } else {
  //     store.dispatch('setUserInfo', _userInfo)
  //     setToken(token)
  //     localStorage.setItem(
  //       store.state.keys.storage.username,
  //       JSON.stringify(_userInfo)
  //     )
  //     return Promise.resolve()
  //   }
  // },
  login(store, userInfo) {
    const { username, password, captcha } = userInfo
    return new Promise((resolve, reject) => {
      getTimeStamp()
        .then((res) => {
          const { timestamp } = res
          const encryptedPassword = encryptPassword({ timestamp, password })
          login({
            username: username.trim(),
            password: encryptedPassword,
            code: captcha
          })
            .then((response) => {
              const { characterStatus } = response
              if (characterStatus !== 1) {
                if (characterStatus === 2) {
                  store.dispatch('resetToken').then(() => {
                    window.location.href = `http://localhost:8080/#/account-status?token=${response.token}`
                  })
                } else if (characterStatus === 3) {
                  store.dispatch('resetToken').then(() => {
                    window.location.href = `http://localhost:8080/#/resubmit-form?token=${response.token}`
                  })
                }
              } else {
                store.dispatch('setUserInfo', response)
                setToken(response.token)
                localStorage.setItem(
                  store.state.keys.storage.username,
                  JSON.stringify(response)
                )
                resolve()
              }
            })
            .catch((error) => {
              reject(error)
            })
        })
        .catch((err) => {
          console.log(err)
        })
    })
  },
  async resetRamKey({ commit }, form) {
    const { timestamp } = await getTimeStamp()
    const password = form.operPassword
    const encryptedPassword = encryptPassword({ timestamp, password })
    return await resetRamKey(form.userId, {
      operPassword: encryptedPassword,
      code: form.code,
      ramUserId: form.userId
    })
  },
  async getRamKey({ commit }, form) {
    const { timestamp } = await getTimeStamp()
    const password = form.operPassword
    const encryptedPassword = encryptPassword({ timestamp, password })
    return await getRamKey(form.userId, {
      operPassword: encryptedPassword,
      code: form.code,
      ramUserId: form.userId
    })
  },
  async changePassword({ commit }, form) {
    const { timestamp } = await getTimeStamp()

    const encryptedNewPassword = encryptPassword({
      timestamp,
      password: form.newPassword
    })
    const encryptedOldPassword = encryptPassword({
      timestamp,
      password: form.oldPassword
    })
    return await changePassword({
      oldPassword: encryptedOldPassword,
      newPassword: encryptedNewPassword,
      code: form.code
    })
  },

  setUserInfo({ commit }, userInfo) {
    commit('SET_TOKEN', userInfo.token)
    commit('SET_NAME', userInfo.name)
    commit('SET_USERNAME', userInfo.username)
    commit('SET_ORG_ID', userInfo.orgId)
    commit('SET_LOGO_URL', userInfo.logoUrl)
    commit('SET_USER_INFO', userInfo)
    commit('SET_PERMISSIONS', userInfo.menuFunctionList)
  },

  getInfo({ commit, dispatch, state }) {
    return new Promise((resolve, reject) => {
      getInfo()
        .then((data) => {
          dispatch('setUserInfo', data)
          const menuList = menuDataTranslate(
            data.platformInfoList[0].menuList,
            'menuId',
            'parentMenuId'
          )
          commit('SET_MENU_LIST', menuList)
          const asyncRoutes = fnAddDynamicMenuRoutes(
            menuList,
            router.options.routes
          )
          resolve(asyncRoutes)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token)
        .then(() => {
          removeToken(state.token) // must remove  token  first
          resetRouter()
          commit('RESET_STATE')
          resolve()
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise((resolve) => {
      removeToken(state.token) // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
